﻿///***************************************************************
/// 시  스  템 : Updms.Car
/// 단위시스템 : CAR
/// 계      층 : Presentation Layer
/// 프로그램명 : Car2011r
/// 파  일  명 : Car2011r.aspx.cs
/// 설      명 : 부품별정비현황
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-09-03 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;
using SMFramework.Web.UI.WebControls.SMGridView;

using Base.Dac;
using Updms.Car.Exe;


namespace Updms.Car
{
	public partial class Car2011r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Car2011r";
		public static readonly string gs_path_info = "/updms/Car/" + PID + ".aspx";

		Global global = new Global();

		//검색
		protected string kw1 = "";	//차량ID
		protected string kw2 = "";	//조회누적거리

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetClientScript();
			SetCarInfo();
			SetLastServiceInfo();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetClientScript

		private void SetClientScript()
		{
			//엔터검색
			txt_search_curr_distance.Attributes.Add("onkeypress", "enterSearchOnPage(event)");

			//onKeyUp이벤트
			txt_search_curr_distance.Attributes.Add("onkeyup", "document.form1.txt_search_curr_distance.value=addComma(document.form1.txt_search_curr_distance.value);");
		}

		#endregion

		#region SetInit, SetSearchParameter, ShowKeywordLabel

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = ddl_search_car_id.SelectedValue;
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						ddl_search_car_id.SelectedValue = kw1;
					}
					
					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						kw2 = txt_search_curr_distance.Text.Trim();
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						txt_search_curr_distance.Text = kw2.ToString();
					}
					break;
				case "SEARCH":
					kw1 = ddl_search_car_id.SelectedValue;
					kw2 = txt_search_curr_distance.Text.Trim();
					break;
			}
		}


		#endregion

		#region 마스터 Method (SetCarInfo, SetLastServiceInfo)

		//차량ID
		private void SetCarInfo()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();
			Car1010m_NT _Car1010m_NT = new Car1010m_NT();

			lo_dp.AddProperty("ls_use_yn", typeof(string), "Y");	//사용여부가 Y인것만

			lo_ds.Merge(_Car1010m_NT.GetCarInfoList(lo_dp));

			ddl_search_car_id.Items.Clear();
			ddl_search_car_id.DataSource = lo_ds;
			ddl_search_car_id.DataMember = "UPDMS_CAR_INFO";
			ddl_search_car_id.DataTextField = "Text_Ex";	//초기주행거리포함
			ddl_search_car_id.DataValueField = "Value";
			ddl_search_car_id.DataBind();
		}

		//마지막 정비정보
		private void SetLastServiceInfo()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();
			Car1012m_NT _Car1012m_NT = new Car1012m_NT();

			lo_dp.AddProperty("ls_car_id", typeof(string), ddl_search_car_id.SelectedValue);

			lo_ds.Merge(_Car1012m_NT.GetLastServiceInfo(lo_dp));
			
			string ls_service_dt = DataSetUtil.GetFldStr(lo_ds, "UPDMS_CAR_SERVICE", "Service_Dt");	//정비일
			string ls_distance = DataSetUtil.GetFldStr(lo_ds, "UPDMS_CAR_SERVICE", "Distance");		//누적주행거리
			
			lbl_search_last_service_info.Text = ls_service_dt +" (누적주행거리:"+ls_distance+"㎞)";

			//조회누적거리에 바인딩
			txt_search_curr_distance.Text = ls_distance;
		}

		//차량ID변경시
		protected void ddl_search_car_id_SelectedIndexChanged(object sender, EventArgs e)
		{
			SetLastServiceInfo();
		}

		#endregion

		#region 디테일 Method ()

		#endregion

		#region 챠트 (SelectProcess, DrawPartsServiceListByDistance, SearchProcess)

		private void SelectProcess()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			//검색
			lo_dp.AddProperty("ls_car_id", typeof(string), kw1);
			lo_dp.AddProperty("li_curr_distance", typeof(int), ConversionUtil.ExtractNumberInt(kw2));

			using (Car2011r_NT _Car2011r_NT = new Car2011r_NT())
			{
				lo_ds.Merge(_Car2011r_NT.GetPartsServiceListByDistance(lo_dp));
			}

			DrawPartsServiceListByDistance(lo_ds);
			GetPartsServiceListByDistance(lo_ds);
		}

		//챠트
		private void DrawPartsServiceListByDistance(DataSet po_ds)
		{
			int li_count = 0;
			DataSet lo_ds = new DataSet();
			
			//초기화
			chart_check.Data.Clear();
			chart_change.Data.Clear();

			//시리즈 갯수
			chart_check.Data.Series = 2;
			chart_change.Data.Series = 2;

			lo_ds.Merge(po_ds);
			li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_CAR_REPORT");
			
			if (li_count > 0)
			{
				for (int i = 0; i < li_count; i++)
				{
					//점검
					chart_check.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CAR_REPORT", (li_count - i) - 1, "Remain_Check_Distance");
					chart_check.Data[1, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CAR_REPORT", (li_count - i) - 1, "Std_Distance");
					
					//교환
					chart_change.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CAR_REPORT", (li_count - i) - 1, "Remain_Change_Distance");
					chart_change.Data[1, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CAR_REPORT", (li_count - i) - 1, "Std_Distance");

					//X축 라벨링
					chart_check.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, "UPDMS_CAR_REPORT", (li_count - i) - 1, "Parts_Nm");
					chart_change.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, "UPDMS_CAR_REPORT", (li_count - i) - 1, "Parts_Nm");
				}
			}
			else
			{
				chart_check.Visible = false;
				chart_change.Visible = false;
			}
		}

		//리스트
		private void GetPartsServiceListByDistance(DataSet po_ds)
		{
			DataSet lo_ds = new DataSet();			

			lo_ds.Merge(po_ds);

			GridView1.DataSource = lo_ds;
			GridView1.DataBind();
		}

		//검색
		private void SearchProcess()
		{
			gs_page_mode = "R";
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			SelectProcess();
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":	// 처음 페이지로드시
					table_chart.Visible = false;
					table_list_set.Visible = false;
					break;
				case "R":	// 검색버튼 클릭시
					table_chart.Visible = true;
					table_list_set.Visible = true;
					break;
			}
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		//RowDataBound
		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onMouseOver", GRID_ONMOUSEOVER_COLOR);
				e.Row.Attributes.Add("onMouseOut", GRID_ONMOUSEOUT_COLOR);
				
				//남은점검거리
				string check_distance_status = GridView1.DataKeys[e.Row.RowIndex].Values["Check_Distance_Status"].ToString();
				e.Row.Cells[8].Attributes.Add("bgcolor", check_distance_status);

				//남은교환거리
				string change_distance_status = GridView1.DataKeys[e.Row.RowIndex].Values["Change_Distance_Status"].ToString();
				e.Row.Cells[9].Attributes.Add("bgcolor", change_distance_status);

				//부품
				string ls_status = String.Empty;
				if (check_distance_status != "#FFFFFF")
				{
					ls_status = check_distance_status;
				}
				if (change_distance_status != "#FFFFFF")
				{
					ls_status = change_distance_status;
				}
				e.Row.Cells[2].Attributes.Add("bgcolor", ls_status);
			}
			
			//머지
			SMGridView sGv = new SMGridView(this.Page, ref GridView1);
			sGv.RowSpanGridView(GridView1, 1);
		}
		
		#endregion

		#region 버튼이벤트

		//챠트
		protected void ibtn_chart_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}

		//엑셀
		protected void ibtn_excel_Click(object sender, EventArgs e)
		{
			SetSearchParameter();

			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_car_id", typeof(string), kw1);
			lo_dp.AddProperty("li_curr_distance", typeof(int), ConversionUtil.ExtractNumberInt(kw2));

			using (Car2011r_NT _Car2011r_NT = new Car2011r_NT())
			{
				lo_ds.Merge(_Car2011r_NT.GetPartsServiceListByDistance(lo_dp));
			}
			string fileName = PID + "(" + global.GetMenuTitle(PID, LANG_SET) + ")";
			ExcelUtil.CreateExcelFileX(this.Page, fileName, lo_ds, SetExcelHeaderX(global.GetColumnListByPgmId(PID, LANG_SET)));
		}
		
		#endregion

		
	}
}
